//
//  DrawerViewController.m
//  LO_MUSIC
//
//  Created by 徐强 on 16/2/24.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 如果没有手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
}

// 设置中间视图控制器
- (void)setRootViewController:(UIViewController *)viewcontroller {
    //如果传进来的viewController是空的直接返回
    if (!viewcontroller) {
        return;
    }
    // 设置中间视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewcontroller;
    [self addChildViewController:viewcontroller];
    // 如果手势交互开着,证明抽屉式打开的
    if (self.tap.enabled) {
        viewcontroller.view.frame = CGRectMake(200, 0, kScreenWidth, kScreenHeight);
    }
    [self.view addSubview:viewcontroller.view];
    
    // 关闭抽屉
    [self showRootViewController:nil];
    
    // 设置导航栏
    [self setNaButton];
}

// 展示中间视图的方法(关闭抽屉)
- (void)showRootViewController:(UIViewController *)viewcontroller {
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, 375, 667);
    }];
    // 手势关掉
    [self.tap setEnabled:NO];
    // rootView交互打开
    self.rootVC.view.userInteractionEnabled = YES;
}

// 打开抽屉
- (void)showleftViewController:(UIViewController *)leftViewController {
    
    if (![self.view .subviews containsObject:self.leftVC.view]) {
        // 插入抽屉的view
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //  动画移动根视图控制器
   [UIView animateWithDuration:0.4 animations:^{
       self.rootVC.view.frame = CGRectMake(200, 0, 375, 667);
    }];
    
    //手势打开
    [self.tap setEnabled:YES];
    // rootview禁止使用
    self.rootVC.view.userInteractionEnabled = NO;
}

// 设置左上角按钮
- (void)setNaButton {
    UINavigationController *na = (UINavigationController *)self.rootVC;
    UIViewController *vc = na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem = ({
//        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"抽屉" style:(UIBarButtonItemStyleDone) target:self action:@selector(openMeun)];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"drawer1"] style:(UIBarButtonItemStyleDone) target:self action:@selector(openMeun)];
        item;
    });
}

// 打开抽屉
- (void)openMeun {
    [self showleftViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}

// tapAction
- (void)tapAction {
    // 关闭抽屉
    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
   self.rootVC.view.userInteractionEnabled = YES;
}

// delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
    }
    return YES;
}



@end
